package com.hcy.app.screen.cipher

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Checkbox
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt


@Composable
fun Contact_Page(){
    Box {
        Column(
            modifier = Modifier.systemBarsPadding().fillMaxSize().background(Color(0xFFE8E096))
        ) {
            // 工具栏
            Row(
                modifier = Modifier.fillMaxWidth().background(Color(0xFF8AECC9)),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = "操作/取消",
                )
                Row {
                    Text(text = "删除")
                    Text(text = "发送")
                }
            }
            // 记录列表
            var itemList = listOf<ContactItem>(
//                ContactItem(1,"妈妈","短信","18571639109","服务端凭证","备注信息"),
//                ContactItem(2,"爸爸","邮箱","33@qq.com","服务端凭证","备注信息"),
//                ContactItem(3,"小明","微信","18571639109","服务端凭证","备注信息"),
            )
            itemList=itemList.flatMap { item->
                List(5){item}
            }
            Column(
                modifier= Modifier.padding(top = 10.dp).weight(1f)
                    .verticalScroll(rememberScrollState())
            ){
                itemList.forEach { it->
                    Contact_Item(it)
                }
            }
        }
        var offset by remember { mutableStateOf(Offset.Zero) }
        // 浮动在右下角的元素,
        // FloatingActionButton 相对于 Button 会强化突出显示的效果
        FloatingActionButton(
            onClick = { /* 点击事件 */ },
            modifier = Modifier
                .align(Alignment.BottomEnd) // 定位到右下角
                .padding(16.dp)
                .offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }
                .pointerInput(Unit) {
                    detectDragGestures { _, dragAmount ->
                        offset += dragAmount
                    }
                },
            containerColor = MaterialTheme.colorScheme.primary
        ) {
            Icon(Icons.Default.Add, "添加")
        }
    }
}

@Composable
private fun Contact_Item(item: ContactItem){
    var checked by remember { mutableStateOf(false) }

    Surface(
        modifier = Modifier.fillMaxWidth().padding(bottom = 10.dp),
        shape = RoundedCornerShape(10.dp)
    ) {
        Row {
            Column(
                modifier = Modifier.weight(1f).padding(10.dp)
            ){
                Row {
                    Text(
                        text = item.nickname,
                        maxLines = 1
                    )
                }
                Row {
                    when(item.contactType){
                        "微信" -> Text("微信：")
                        "短信" -> Text("短信：")
                        "邮件" -> Text("邮件：")
                        else -> Text("未知通知方式：")
                    }
                    Text(item.contactCode)
                }
            }
            Checkbox(
                checked = checked,
                onCheckedChange = {checked=it}
            )
        }
    }
}
